package com.shops.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author gexiang
 * @Description:
 * @date 2020/7/1813:06
 */
public class Forward extends HttpServlet {
    int count;
    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

//        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/fs");
//        requestDispatcher.include(req,res);
//        PrintWriter writer = res.getWriter();
//        writer.println("这是forward");
//        requestDispatcher.forward(req,res);
        PrintWriter out = res.getWriter();
//        res.setContentType("text/html");
//        res.setCharacterEncoding("utf-8");
        Cookie[] cookies = req.getCookies();
        count++;
        out.println("这是"+count+"请求");
        if (cookies == null){
            out.println("没有cookie" );
            Cookie gexiang = new Cookie("gexiang", "123");
            gexiang.setMaxAge(60);
            res.addCookie(gexiang);
        }else {
            for (Cookie cookie :
                    cookies) {
                out.println(cookie.getName()+"---------"+cookie.getValue());
            }
        }

        // 获取session对象
        HttpSession session = req.getSession(true);
        session.setAttribute("ge","dfs");
        session.setMaxInactiveInterval(5);

//
//        out.println("isNew: " + session.isNew() +"<br/>");
//        out.println("getId: " + session.getId() +"<br/>");
//        out.println("getCreationTime: " + new java.util.Date(session.getCreationTime()) +"<br/>");
//        out.println("getLastAccessedTime: " + new java.util.Date(session.getLastAccessedTime()) +"<br/>");
//        out.println("getMaxInactiveInterval: " + session.getMaxInactiveInterval() +"<br/>");
//        out.println("<hr/>");

        // 演示cookie被禁用后的会话跟踪
        String path = res.encodeURL("/servlet_day3/session");
        out.println("<a href='"+path+"'>session</a>");


        // 演示获取ServletContext中的命名属性
//        ServletContext ctx = super.getServletContext();
//        ServletContext servletContext = super.getServletContext();
//        ctx.setAttribute("str", "HAHA");
//        Object obj = ctx.getAttribute("str");
//        out.println("ctx中的命名属性： " + obj);
//        Object lixinggang = ctx.getAttribute("1");
//        System.out.println(lixinggang);
//        ServletContext servletContext = req.getSession(true).getServletContext();
//        Object attribute = servletContext.getAttribute("1");
//        System.out.println("attribute = " + attribute);
        InputStream inputStream = req.getSession(true).getServletContext().getResourceAsStream("C:\\shops_start\\resources\\db.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String property = properties.getProperty("jdbc.url");
        System.out.println("property = " + property);


    }


}
